<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-form-model ref="ruleForm" :model="model" :rules="rules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="产出框码"
          prop="outputCode"
          hasFeedback
        >
          <a-input
            placeholder="请输入产出框码"
            v-model.trim="model.outputCode"
          />
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="合格数量"
          prop="outputCount"
          hasFeedback
        >
          <a-input
            placeholder="请输入合格数"
            v-model.trim="model.outputCount"
          />
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="不合格数量"
          prop="badCount"
          hasFeedback
        >
          <a-input
            placeholder="请输入不合格数量"
            v-model.trim="model.badCount"
          />
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="不合格原因"
          hasFeedback
        >
          <a-radio-group v-model="model.badCountReason" :options="badOptions">
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          prop="outputRemark"
          hasFeedback
        >
          <a-input placeholder="请输入备注" v-model.trim="model.outputRemark" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import { isNumber } from "@/unit/rule.js";
export default {
  props: {
    wsRelationId: {
      type: String,
    },
    processNum: {
      type: String,
    },
    sort: {
      type: String,
    },
    userId: {
      type: String,
    },
    worksheetId: {
      type: String,
    },
  },
  data() {
    let validator1 = (rule, value, callback) => {
      console.log(Number(value) + Number(this.model.outputCount) == 0);
      if (!value) {
        callback(new Error("不能为空"));
      } else if (Number(value) + Number(this.model.outputCount) == 0) {
        callback(new Error("合格数和不合格数不能为0"));
      } else {
        callback();
      }
    };
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      model: {},
      goodsOption: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      rules: {
        outputCode: [{ required: true, message: "必填!" }],
        outputCount: [
          { required: true, message: "必填!" },
          { validator: isNumber, trigger: "change" },
        ],
        badCount: [
          { required: true, message: "必填!" },
          { validator: validator1, trigger: "blur" },
          { pattern: new RegExp(/^[0-9]*$/), message: "不能为小数" },
        ],
        badCountReason: [{ required: true, message: "必填!" }],
      },
      badOptions: [],
    };
  },
  mounted() {},
  computed: {},
  methods: {
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.getDictFindByCode();
      this.visible = true;
      this.model = Object.assign({}, record);
    },
    // 获取不合格原因的数据
    async getDictFindByCode(parentCode, key) {
      const res = await api.DictFindByCode({
        parentCode: "ReasonsForProducts",
      });
      this.badOptions = res.data.map((item) => {
        return {
          label: item.dictName,
          value: item.id,
        };
      });
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    async submitVulFun() {
      this.confirmLoading = true;
      this.model.wsRelationId = this.wsRelationId;
      this.model.worksheetId = this.worksheetId;
      this.model.userId = this.userId;
      this.model.sort = this.sort
      let res;
      if (this.model.id) {
        res = await api.outputInfoUpdate(this.model); //修改
      } else {
        res = await api.outputInfoAdd(this.model); //新增
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.model = {};
      this.visible = false;
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },
  },
};
</script>

<style scoped lang="less">
::v-deep .ant-form-item-children-icon {
  display: none !important;
}

